import { Entity, Column, OneToMany, JoinColumn, ManyToOne } from "typeorm";

import {
  VARCHAR50_PG,
  VARCHAR255_PG,
  VARCHAR_PG,
  INTEGER_PG,
  Media,
  User,
  MediaGroup,
  MediaGateway,
  ENUM_MEDIA,
  RefVoucherMedia,
  RefTonOrderMedia,
  MediaGroupGateway,
} from "qqlx-core";

import { TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar, TransformerVarchar50, TransformerVarchar255 } from "@lib/orm.transfor.string";
import { TransformerEnum } from "@lib/orm.transfor.enum";

import { IdIntroSchema } from "@/alpha/ext.id.intro";

@Entity({ name: MediaGroupGateway.relations })
export class MediaGroupSchema extends IdIntroSchema implements MediaGroup {
  @OneToMany("MediaSchema", (s: Media) => s.justMediaGroup)
  joinMediaList!: Media[];
}

@Entity({ name: MediaGateway.relations })
export class MediaSchema extends IdIntroSchema implements Media {
  @Column({ transformer: new TransformerEnum(Object.values(ENUM_MEDIA), ENUM_MEDIA.NONE) })
  type: ENUM_MEDIA = ENUM_MEDIA.NONE;

  @Column({ transformer: new TransformerVarchar255() })
  host: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar() })
  path: VARCHAR_PG = "";

  @Column({ transformer: new TransformerInteger() })
  size: INTEGER_PG = -1;

  @Column({ transformer: new TransformerVarchar255() })
  mime: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar50() })
  refCreatorId!: VARCHAR50_PG;

  @ManyToOne("MediaGroupSchema", (s: MediaGroup) => s.joinMediaList)
  @JoinColumn({ name: "refMediaGroupId", referencedColumnName: "id" })
  justMediaGroup!: MediaGroup;

  @Column({ transformer: new TransformerInteger() })
  refMediaGroupId!: INTEGER_PG;

  @ManyToOne("UserSchema")
  @JoinColumn({ name: "refCreatorId", referencedColumnName: "uuid32" })
  justCreator!: User;

  @OneToMany("RefTonOrderMediaSchema", (s: RefTonOrderMedia) => s.justMedia)
  joinRefTonOrderMediaList!: RefTonOrderMedia[];

  @OneToMany("RefVoucherMediaSchema", (s: RefVoucherMedia) => s.justMedia)
  joinRefVoucherMediaList!: RefVoucherMedia[];
}
